//
//  PEMMotionCongruenceController.swift
//  RelationshipIntegration
//
//  Created by iOS-Czz on 2023/11/16.
//

import UIKit

class PEMMotionCongruenceController: PEMBaseViewController {
    @IBOutlet var tableView: UITableView!
    
    @IBOutlet var monthLabel: UILabel!
    @IBOutlet var invitedLabel: UILabel!
    @IBOutlet var inviteLabel: UILabel!
    @IBOutlet var pendingLabel: UILabel!
    lazy var viewModel: PEMMotionCongruenceViewModel = {
        return PEMMotionCongruenceViewModel()
    }() 
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.monthLabel.text = Date().toString(format: "yyyy.MM")
        loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
       setTableView()
        
        // Do any additional setup after loading the view.
    }
    func setTableView(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "PEMBeInvitedListCell", bundle: .main), forCellReuseIdentifier: "PEMBeInvitedListCell")
        self.tableView.register(UINib(nibName: "PEMMotionMeInvitedCell", bundle: .main), forCellReuseIdentifier: "PEMMotionMeInvitedCell")
        self.tableView.register(UINib(nibName: "PEMEmptyCell", bundle: .main), forCellReuseIdentifier: "PEMEmptyCell")
    }
    @IBAction func gotoAddCheckAppointment(_ sender: Any) {
        self.navigationController?.pushViewController(PEMMotionCongruenceAddController(), animated: true)
    }
    @IBAction func clickLastMonthAction(_ sender: Any) {
        self.monthLabel.text = self.monthLabel.text?.getPreviousOrNextMonth(monthsToAdd: -1)
        loadData()
    }
    @IBAction func clickNextMonthAction(_ sender: Any) {
        self.monthLabel.text = self.monthLabel.text?.getPreviousOrNextMonth(monthsToAdd: 1)
        loadData()
    }
    func loadData(){
        self.viewModel.getPageData(self.monthLabel.text!, onSuccess: { models in
            self.tableView.reloadData()
            self.pendingLabel.text = self.viewModel.pending
            self.inviteLabel.text = self.viewModel.invite
            self.invitedLabel.text = self.viewModel.invited
        })
    }
}
extension PEMMotionCongruenceController:UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count > 0 ? self.viewModel.dataSource.count:1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if self.viewModel.dataSource.count > 0 {
            let model = self.viewModel.dataSource[indexPath.row]
            if model.type == 1 {
                let cell:PEMMotionMeInvitedCell = tableView.dequeueReusableCell(withIdentifier: "PEMMotionMeInvitedCell") as! PEMMotionMeInvitedCell
                cell.model = model
                cell.refreshBlock = { [self] in
                    self.loadData()
                }
                return cell
            }else{
                let cell:PEMBeInvitedListCell = tableView.dequeueReusableCell(withIdentifier: "PEMBeInvitedListCell") as! PEMBeInvitedListCell
                cell.model = model
                cell.refreshBlock = { [self] in
                    self.loadData()
                }
                return cell
            }
        }else{
            let cell:PEMEmptyCell = tableView.dequeueReusableCell(withIdentifier: "PEMEmptyCell") as! PEMEmptyCell
            return cell
        }
    }
}
